package com.hs.jpademo1.controller;

import com.hs.jpademo1.entity.User;
import com.hs.jpademo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @ClassName UserController
 * @Description TODO
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    UserService service;

    @PostMapping("/login")
    public String login(@RequestBody User user, HttpSession session){
        try {
            User login = service.login(user);
            session.setAttribute("user",login);
        }catch (RuntimeException e) {
            return e.getMessage();
        }
        return "登录成功";
    }

    @GetMapping("/test")
    public String test(){
        if( 1 == 1){
            throw new RuntimeException();
        }
        return "test";
    }
}
